package com.houdoudou.affair.jsp;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.houdoudou.bean.DatabaseAndTable;
import com.houdoudou.dao.ConnFactory;
import com.houdoudou.dao.DatabaseAndTableDao;

public class DatabaseAffairJsp {

/*------------------------------------------创建数据库-------------------------------------------------*/	
	
	public static void createDatabase(HttpServletRequest req, HttpServletResponse resp){		
		  //调用 dao处理事务
		   Connection conn= ConnFactory.getConn();
		   String errorInfo;
			boolean result= new DatabaseAndTableDao(conn).createDatabase();	
			 ConnFactory.closeConn(conn);			 
			 //处理事务结果，赋值
			 if(result){
				errorInfo="创建成功";			
			       }else{		
			    	   errorInfo="创建失败";	
			       }
			 //处理事务结果跳转
			 //处理事务结果跳转------------------
			 String jumpJspPath="/programmer/initDatabase.jsp";
			 Common.forwardToJsp(req, resp, jumpJspPath, errorInfo);
			
	}
	
/*------------------------------------------创建数据表-------------------------------------------------*/	
	
	public static void createTable(HttpServletRequest req, HttpServletResponse resp){		
		  //调用 dao处理事务
		   Connection conn= ConnFactory.getConn();
		   String sql="";
		   String errorInfo = null;
		   String tableName= req.getParameter("name");
		   System.out.println("create table name="+tableName);
			 if(tableName.equals("financer")){
				sql=DatabaseAndTable.sqlCreateTbFinancer;
			 }
          if(tableName.equals("programmer")){
        	 sql=DatabaseAndTable.sqlCreateTbProgrammer;
			 }
          if(tableName.equals("owner")){
         	 sql=DatabaseAndTable.sqlCreateTbOwner;
 			 }
          if(tableName.equals("machine")){
          	 sql=DatabaseAndTable.sqlCreateTbMachine;
  			 }
          if(tableName.equals("box")){
           	 sql=DatabaseAndTable.sqlCreateTbBox;
   			 }
          if(tableName.equals("supplier")){
           	 sql=DatabaseAndTable.sqlCreateTbSupplier;
   			 }
          if(tableName.equals("index")){
            	 sql=DatabaseAndTable.sqlCreateTbIndex;
    			 }
          if(tableName.equals("product")){
         	 sql=DatabaseAndTable.sqlCreateTbProduct;
 			 }
          if(tableName.equals("branch")){
          	 sql=DatabaseAndTable.sqlCreateTbBranch;
  			 }
          if(tableName.equals("customer")){
           	 sql=DatabaseAndTable.sqlCreateTbCustomer;
   			 }
          if(tableName.equals("distribute")){
            	 sql=DatabaseAndTable.sqlCreateTbDistribute;
    			 }
          if(tableName.equals("order")){
         	 sql=DatabaseAndTable.sqlCreateTbOrder;
 			 }
          if(tableName.equals("refund")){
          	 sql=DatabaseAndTable.sqlCreateTbRefund;
  			 }
          if(tableName.equals("orderProduct")){
          	 sql=DatabaseAndTable.sqlCreateTbOrderProduct;
  			 }
          if(tableName.equals("verifyTelnum")){
           	 sql=DatabaseAndTable.sqlCreateTbVerifyTelnum;
   			 }
          if(tableName.equals("courier")){
            	 sql=DatabaseAndTable.sqlCreateTbCourier;
    			 }
          if(tableName.equals("maintenancer")){
            	 sql=DatabaseAndTable.sqlCreateTbMaintenancer;
    			 }
          if(tableName.equals("boxUsage")){
         	 sql=DatabaseAndTable.sqlCreateTbBoxUsage;
 			 }
			boolean result=  new DatabaseAndTableDao(conn).createTable(sql);	
			 ConnFactory.closeConn(conn);			 
			 //处理事务结果，赋值
			 if(result){
				 errorInfo="创建成功";			
			       }else{		
			    	   errorInfo="创建失败";			
			       }
			 //处理事务结果跳转			
			 String jumpJspPath=req.getContextPath()+"/programmer/initDatabase.jsp";
			 Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);	
	}	
	
	
}
